# -*- coding:UTF-8 -*-

from logging import Filterer


class MyLogFilter(Filterer):
    def filter(self, record):
        '''
        对term终端消息进行日志屏蔽处理，因为当在宿主机上tail -f walle自身日志文件时，会造成恶性循环，终端消息日志会写到日志文件，\
        同时日志文件追加内容又转送到客户端再次写日志
        :param record:
        :return:
        '''
        if record.name == 'engineio.server' and record.module == 'socket' and record.funcName == 'send' \
            and len(record.args) == 3 and record.args[1] == 'MESSAGE' \
            and 0 <= record.args[2].find('/webterm,["term",') <= 10:
            return False
        if record.name == 'engineio.server' and record.module == 'socket' and record.funcName == 'receive' \
            and len(record.args) == 3 and record.args[1] == 'MESSAGE' \
            and 0 <= record.args[2].find('/webterm,["term",') <= 10:
            return False
        if record.name == 'socketio.server' and record.module == 'server' and record.funcName == 'emit' \
            and len(record.args) == 3 and record.args[0] == 'term' and record.args[2] == '/webterm':
            return False
        if record.name == 'socketio.server' and record.module == 'server' and record.funcName == '_handle_event' \
            and len(record.args) == 3 and record.args[0] == 'term' and record.args[2] == '/webterm':
            return False
        if record.name == 'flask.app' and record.module == 'webssh' and record.funcName == 'on_term':
            return False
        return True
